###### Appendix: Table C1
###### Variables Used for Extreme Bounds Analysis
gc()
rm(list = ls())
set.seed(12345)
setwd(dirname(rstudioapi::getActiveDocumentContext()$path)) # Note: if you are not using R Studio this command will not work, set WD to source file location manually

source("functions.R")
source("data/working_eba_function_current.R") # Ensure that the updated function with min.select and max.select is incorporated
require(pacman)
pacman::p_load(plm, sandwich, clubSandwich, lmtest, 
               ggplot2, numbers, dplyr, Hmisc, formattable, 
               htmltools, webshot, ggpubr, panelView, fixest, plyr, 
               scales, DataCombine)
require(webshot)
require(stringr)
date <- paste(data.table::tstrsplit(Sys.Date(), "-")[c(2:3, 1)], collapse = "")
load("data/cleaned/vat_panel_cleaned15Nov.RData")


################################################################################
### Appendix Table C1 - Variables Used for Extreme Bounds Analysis
################################################################################
dfPropmiss <- as.data.frame(propMiss)
mainVars = c("rev_ex_gr_ex_sc",
              "comb_indirect",
              "direct_ex_sc_ex_rt",
              "nontax") 
addVars = dfVar$var_name[dfVar$var_group == "additional"] 
keyVars = dfVar$var_name[dfVar$var_group == "key"]

varNames <- dfVar$var_name[dfVar$var_name %in% c(mainVars, 
                                                 addVars, 
                                                 keyVars)]
dfPropmisslong <- dfPropmiss %>% 
    tidyr::gather(all_of(varNames), key = "var_name", value = "propmiss")

dfPropmisslong <- dfPropmisslong %>% 
    select(var_name, 
           propmiss)

dfMainmiss <- dfPropmisslong %>% 
    left_join(dfVar, by = "var_name")

dfMainmissrename <- dfMainmiss %>% 
    dplyr::rename(Source = source) %>% 
    dplyr::rename("Variable" = label) %>% 
    dplyr::rename("Group" = var_group) %>% 
    dplyr::rename("% Missing" = propmiss) %>% 
    select("Variable", "Group", "Source", "% Missing")


dfMainmissrename$`% Missing` <- dfMainmissrename$`% Missing`*100

dfMainmissrename$`% Missing` <- round(dfMainmissrename$`% Missing`, digits = 1)


expTab <- formattable(dfMainmissrename[, c("Variable", "Group", "Source", "% Missing")], 
                      align = c("l", "l", "l", "c"))

export_formattable(expTab, file = "figures/appendix/appendix_table_c1.pdf", 
                   height = "100%", width = "45%")

